﻿using System;
using System.IO;
using System.Runtime.Serialization;

namespace _039_将数据协定序列化为JSON格式
{
    class Program
    {
        static void Main(string[] args)
        {
            //执行序列化并将结果输出到文件中
            using (FileStream 序列化=new FileStream("data.json",FileMode.Create))
            {
                简易示例表 输出 = new 简易示例表
                {
                    TestVal1 = 333.651d,
                    TestVal2 = DateTime.Now,
                    TestVal3 = 797001
                };
                DataContractSerializer 数据操作 = new DataContractSerializer(typeof(简易示例表));
                数据操作.WriteObject(序列化, 输出);
            }


            //执行反序列化
            using (FileStream 反序列化=new FileStream("data.json",FileMode.Open))
            {
                DataContractSerializer 操作手柄 = new DataContractSerializer(typeof(简易示例表));
                简易示例表 数据 = 操作手柄.ReadObject(反序列化) as 简易示例表;
                Console.Write ($"{nameof(简易示例表.TestVal1)}=={数据.TestVal1}\n{nameof(简易示例表.TestVal2)}={数据.TestVal2}\n{nameof(简易示例表.TestVal3)}={数据.TestVal3}");
            }
        }
    }

    [DataContract]
    public class 简易示例表
    {
        [DataMember(Name = "val_1")]
        public double TestVal1;
        [DataMember(Name = "val_2")]
        public DateTime TestVal2;
        [DataMember(Name = "val_3")]
        public uint TestVal3;
    }
}
